$(function () {
    $("#pro_datagrid").datagrid({
        fitColumns: true,
        fit: true,
        toolbar: '#pro_toolbar',
        url: '/promissGroup/selectAll.do',
        pagination: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '机构代码', field: 'code',align:'center', width: '200'},
            {title: '机构名称', field: 'promise' ,align:'center', width: '200', formatter: function (value, row, index) {
                    return value?value.name:"";
                }},
            {title: '法人代表', field: 'legal' ,align:'center', width: '200'},
            {title: '法人身份证', field: 'idcard' ,align:'center', width: '200'},
            {title: '联系方式', field: 'tel' ,align:'center', width: '200'},
            {
                title: '合作状态', field: 'status' ,align:'center', width: '200', formatter: function (value, row, index) {
                    return value ? "<font color='green'>是</font>" : "<font color='red'>否</font>"
                }
            }
        ]]

    })

    $("#pro_dialog").dialog({
        width: 350,
        height: 470,
        buttons: '#pro_buttons',
        closed: true,
        onClose: function () {
            $("#pro_form").form("clear")
        }
    })


    var methodObj = {
        //添加按钮
        add: function () {
            $("#pro_dialog").dialog("open");
            $("#pro_dialog").dialog('setTitle', '新增机构');
        },
        //编辑按钮
        edit: function () {
            var data = $("#pro_datagrid").datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            $("#pro_form").form('load', data);


            $("#pro_dialog").dialog("open");
            $("#pro_dialog").dialog('setTitle', '编辑机构');
        },
        //解除按钮
        status: function () {
            var row = $("#pro_datagrid").datagrid("getSelected");
            var text = $("#btn_state").text();
            if (!row) {
                $.messager.alert("温馨提示", "请选择要设置的数据", "info");
                return;
            }
            $.messager.confirm("温馨提示", "你确定要设置" + text + "吗？", function (r) {
                if (r) {
                    $.get("/promissGroup/status.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "设置成功", "info", function () {
                                $("#pro_datagrid").datagrid("reload");
                            })
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    }) ;
                }
            })
        },
        //刷新按钮
        reload: function () {
            $("#pro_datagrid").datagrid("reload");
        },
        //取消按钮
        cancel: function () {
            $("#pro_dialog").dialog("close");
            $("#pro_changePwd").dialog("close");
        },
        query: function () {
            var keyword = $("#keyword").textbox("getValue");
            var beginDate = $("#beginDate").textbox("getValue");
            var endDate = $("#endDate").textbox("getValue");
            var qoDept = $("#qoDept").textbox("getValue");

            $("#pro_datagrid").datagrid('load', {
                keyword: keyword,
                beginDate: beginDate,
                endDate: endDate,
                deptId: qoDept
            });
        },
        //添加保存按钮
        save: function (val) {
            var url = $(val).data("url");
            var from = $(val).data("from");

            $("#" + from).form('submit', {
                url: '/promissGroup/'+url+'.do',
                success: function (data) {
                    console.log(data)
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.cancel();
                            $("#pro_datagrid").datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                }
            })
        }
    }

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd](this);
    })

})

